home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Workspace / Bypass / BypassController.m < prev    next >
Text File  |  1995-06-12  |  7KB  |  213 lines

  1.  
  2. /*
  3.     Copyright 1993  Jeremy Slade.
  4.  
  5.     You are free to use all or any parts of the Bypass project
  6.     however you wish, just give credit where credit is due.
  7.     The author (Jeremy Slade) shall not be held responsible
  8.     for any damages that result out of use or misuse of any
  9.     part of this project.
  10.  
  11. */
  12.  
  13. /*
  14.     Project: Bypass
  15.     
  16.     File: BypassController.m
  17.     
  18.     Description:
  19.     
  20.     BypassController does all the work of the Bypass program.  It checks the options specified on the command line to see what action is to be taken, and then does it.  The purpose of Bypass is to temporarily disable the NeXTSTEP Dock, to keep any applications on the Dock from being autolaunched when a user logs in.  The dock will then be reenabled when they log out.
  21.     Bypass operates by setting the DockLaunchFlags preference setting of the Workspace.  This settting controls which of the applications on the Dock will be autolaunched; by setting it to 1, nothing but the Workspace will be launched when the user logs in.  The actual setting is stored in Bypass' preference settings, as is restored when the user logs out.
  22.     Bypass can only perform properly when set up to run as the user's login and logout hooks.  See the man pages on loginWindow, and the NeXTSTEP Admin reference manual for more info on this.  Optionally, you can use the LoginHook and LogoutHook programs, also by Jeremy Slade, to help you do this.
  23.     
  24.     Original Author: Jeremy Slade
  25.     
  26.     Revision History:
  27.         Created
  28.             JGS Sat Apr 10 14:28:17 MDT 1993
  29.  
  30. */
  31.  
  32. #import "BypassController.h"
  33. #import <appkit/nextstd.h>
  34. #import <defaults/defaults.h>
  35. #import <dpsclient/dpsclient.h>
  36. #import <dpsclient/wraps.h>
  37. #import <stdio.h>
  38. #import <string.h>
  39. #import <time.h>
  40.  
  41.  
  42. #define TARGET_APPNAME        "Workspace"
  43. #define TARGET_DEFAULT        "DockLaunchFlags"
  44. #define OUR_APPNAME            "Bypass"
  45. #define REPLACEMENT_FLAGS    "1"
  46. #define DONOTHING_FLAGS        "0"
  47.  
  48. #define OPT_DISABLE            "Disable"
  49. #define OPT_ENABLE            "Enable"
  50. #define DONOTHING            0
  51. #define DISABLE_DOCK        1
  52. #define ENABLE_DOCK            2
  53.  
  54. #define BUTTON_WAIT            2
  55.  
  56.  
  57.  
  58. @implementation BypassController : Object
  59.  
  60.  
  61. - (int)run
  62. /*
  63.     This method is the 'main loop' for Bypass.  It checks the command line parameters, accessed through the NXArgc and NXArgv globals, to see what it is supposed to do.  Bypass can take one of two actions: Enable the Dock, or Disable the Dock.  The usage for Bypass is: "Bypass [ Enable | Disable ]".  This method will actually loop through all command line parameters looking for either "Enable" or "Disable".  When one is found, the action variable is set to either DISABLE_DOCK or ENABLE_DOCK, and the appropriate method is then called to perform the action.  This returns the return value of the action method, or 1 of no action was taken.
  64. */
  65. {
  66.     int i;
  67.     int action = DONOTHING;
  68.     
  69.     // Check command-line arguments to see what action to take.
  70.     for ( i=1; i<NXArgc; i++ ) {
  71.         if ( !strcasecmp ( NXArgv[i], OPT_DISABLE ) ) {
  72.             action = DISABLE_DOCK;
  73.             break;
  74.         } else
  75.         if ( !strcasecmp ( NXArgv[i], OPT_ENABLE ) ) {
  76.             action = ENABLE_DOCK;
  77.             break;
  78.         }
  79.     }
  80.     
  81.     // Perform the action
  82.     switch ( action ) {
  83.         case DISABLE_DOCK:    return ( [self disableDock] );
  84.         case ENABLE_DOCK:    return ( [self enableDock] );
  85.     }
  86.     
  87.     // No valid parameters were specified...
  88.     printf ( "Invalid arguments. Usage: Bypass [Enable|Disable]\n" );
  89.     return ( 1 ); // This is a error condition
  90. }
  91.  
  92.  
  93.  
  94. - (int)disableDock
  95. /*
  96.     This method disables all of the autolaunched applications in the dock, by setting the Workspace's DockLuanchFlags preference setting to 1.  It stores the originally setting of the flag in its own preference owned by Bypass, so that it can be restored later by -enableDock.  The dock is only enabled if one of the mouse buttons is depressed.  It will wait for a period, determined by BUTTON_WAIT, for the mouse button to be pressed.
  97. */
  98. {
  99.     const char *targetDefault;
  100.     char theFlags[11];
  101.     DPSContext context;
  102.     int buttonDown;
  103.     
  104.     // Only disables if the mouse button is being held down...
  105.     context = DPSCreateContext ( NULL, NULL, NULL, NULL );
  106.             // Create a basic WindowServer context so that we can
  107.             // call PSbuttondown()
  108.             
  109.     // Check if either mouse button is down...
  110.     PSbuttondown ( &buttonDown );
  111.     if ( !buttonDown ) PSrightbuttondown ( &buttonDown );
  112.     if ( !buttonDown ) { // Button is not down when we started
  113.         // Wait up to BUTTON_WAIT secs for it to go down
  114.         long now, start;
  115.         now = start = time ( 0 );
  116.         while ( !buttonDown && (now - start) <= BUTTON_WAIT ) {
  117.             PSbuttondown ( &buttonDown );
  118.             if ( !buttonDown ) PSrightbuttondown ( &buttonDown );
  119.             now = time ( 0 );
  120.         }
  121.     }
  122.     
  123.     if ( !buttonDown ) { // The mouse button is not down
  124.         // Save the DONOTHING_FLAGS under OUR_APPNAME, so next time
  125.         // enableDock gets called, nothing will happen
  126.         NXWriteDefault ( OUR_APPNAME, TARGET_DEFAULT, DONOTHING_FLAGS );
  127.         
  128.         // This is not an error condition, exit normally...
  129.         return ( 0 );
  130.     }
  131.     
  132.     
  133.     /*
  134.      * Get the TARGET_DEFAULT for the TARGET_APPNAME, save it under
  135.      * OUR_APPNAME, and replace it with REPLACEMENT_FLAGS
  136.      */
  137.  
  138.     // Get the default...
  139.     targetDefault = NXGetDefaultValue ( TARGET_APPNAME, TARGET_DEFAULT );
  140.     if ( !targetDefault ) {
  141.         printf ( "Couldn't read %s default %s\n",
  142.             TARGET_APPNAME, TARGET_DEFAULT );
  143.         return ( -1 );
  144.     }
  145.     strncpy ( theFlags, targetDefault, 10 );
  146.     
  147.     // Save it under OUR_APPNAME...
  148.     if ( !NXWriteDefault ( OUR_APPNAME, TARGET_DEFAULT, theFlags ) ) {
  149.         printf ( "Couldn't write %s default %s (Value = %s)\n",
  150.             OUR_APPNAME, TARGET_DEFAULT, theFlags );
  151.         return ( -1 );
  152.     }
  153.     
  154.     // Replace the default with REPLACEMENT_FLAGS
  155.     if ( !NXWriteDefault ( TARGET_APPNAME, TARGET_DEFAULT,
  156.             REPLACEMENT_FLAGS ) ) {
  157.         printf ( "Could'nt write %s default %s ( Value = %s)\n",
  158.             TARGET_APPNAME, TARGET_DEFAULT, theFlags );
  159.         return ( -1 );
  160.     }
  161.     
  162.     return ( 0 ); // Everything went ok...
  163. }
  164.  
  165.  
  166.  
  167. - (int)enableDock
  168. /*
  169.     Reenebles the dock after having been disabled by -disableDock, by restoring the original settting of Workspace's DockLaunchFlags preference.  If the dock wasn't previously disabled, this method does nothing.
  170. */
  171. {
  172.     const char *targetDefault;
  173.     char theFlags[11];
  174.     
  175.     /*
  176.      * Get the TARGET_DEFAULT under OUR_APPNAME, set via a previous call
  177.      * to disableDock, the save it under TARGET_APPNAME
  178.      */
  179.      
  180.     targetDefault = NXGetDefaultValue ( OUR_APPNAME, TARGET_DEFAULT );
  181.     if ( !targetDefault ) {
  182.         printf ( "Couldn't read %s default %s\n",
  183.             OUR_APPNAME, TARGET_DEFAULT );
  184.         return ( -1 );
  185.     }
  186.     strncpy ( theFlags, targetDefault, 10 );
  187.     
  188.     // Check if the flags to be restored are the DONOTHING_FLAGS.
  189.     // If this is the case, nothing should be done.
  190.     if ( !strcmp ( theFlags, DONOTHING_FLAGS ) ) {
  191.         // This is not an error condition, exit normally
  192.         return ( 0 );
  193.     }
  194.     
  195.     // Save it under TARGET_APPNAME...
  196.     if ( !NXWriteDefault ( TARGET_APPNAME, TARGET_DEFAULT, theFlags ) ) {
  197.         printf ( "Couldn't write %s default %s (Value = %s)\n",
  198.             TARGET_APPNAME, TARGET_DEFAULT, theFlags );
  199.         return ( -1 );
  200.     }
  201.     
  202.     // Write DONOTHING_FLAGS under OUR_APPNAME so it will be ignore
  203.     // the next time around...
  204.     NXWriteDefault ( OUR_APPNAME, TARGET_DEFAULT, DONOTHING_FLAGS );
  205.     
  206.     return ( 0 ); // Everything went ok...
  207. }
  208.  
  209.  
  210.  
  211. @end
  212.  
  213.